using SqlSugar;
using Rabbit3.Api.Models;
using Rabbit3.Api.Dto;
using AutoMapper;

namespace Rabbit3.Api.Services
{
    /// <summary>
    /// 深加工信息服务接口
    /// </summary>
    public interface IProcessingService
    {
        /// <summary>
        /// 根据溯源信息ID获取深加工信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>深加工信息</returns>
        Task<ProcessingResponseDto?> GetByTraceabilityIdAsync(int traceabilityId);

        /// <summary>
        /// 根据ID获取深加工信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>深加工信息</returns>
        Task<ProcessingResponseDto?> GetByIdAsync(int id);

        /// <summary>
        /// 创建深加工信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<ProcessingResponseDto> CreateAsync(CreateProcessingDto createDto);

        /// <summary>
        /// 更新深加工信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<ProcessingResponseDto?> UpdateAsync(UpdateProcessingDto updateDto);

        /// <summary>
        /// 删除深加工信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteAsync(int id);

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto);

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto);

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteEnterpriseAsync(int id);

        /// <summary>
        /// 创建供应商信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<SupplierResponseDto> CreateSupplierAsync(CreateSupplierDto createDto);

        /// <summary>
        /// 更新供应商信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<SupplierResponseDto?> UpdateSupplierAsync(UpdateSupplierDto updateDto);

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteSupplierAsync(int id);
    }

    /// <summary>
    /// 深加工信息服务实现
    /// </summary>
    public class ProcessingService : IProcessingService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;

        public ProcessingService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        /// <summary>
        /// 根据溯源信息ID获取深加工信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>深加工信息</returns>
        public async Task<ProcessingResponseDto?> GetByTraceabilityIdAsync(int traceabilityId)
        {
            var entity = await _db.Queryable<ProcessingInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<ProcessingResponseDto>(entity);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && x.StageType == "深加工" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            // 获取供应商信息
            var supplierInfos = await _db.Queryable<SupplierInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && !x.IsDeleted)
                .ToListAsync();

            response.SupplierInfos = _mapper.Map<List<SupplierResponseDto>>(supplierInfos);

            return response;
        }

        /// <summary>
        /// 根据ID获取深加工信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>深加工信息</returns>
        public async Task<ProcessingResponseDto?> GetByIdAsync(int id)
        {
            var entity = await _db.Queryable<ProcessingInfo>()
                .Where(x => x.Id == id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<ProcessingResponseDto>(entity);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == entity.TraceabilityId && x.StageType == "深加工" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            // 获取供应商信息
            var supplierInfos = await _db.Queryable<SupplierInfo>()
                .Where(x => x.TraceabilityId == entity.TraceabilityId && !x.IsDeleted)
                .ToListAsync();

            response.SupplierInfos = _mapper.Map<List<SupplierResponseDto>>(supplierInfos);

            return response;
        }

        /// <summary>
        /// 创建深加工信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<ProcessingResponseDto> CreateAsync(CreateProcessingDto createDto)
        {
            var entity = _mapper.Map<ProcessingInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<ProcessingResponseDto>(entity);
        }

        /// <summary>
        /// 更新深加工信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<ProcessingResponseDto?> UpdateAsync(UpdateProcessingDto updateDto)
        {
            var entity = await _db.Queryable<ProcessingInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<ProcessingResponseDto>(entity);
        }

        /// <summary>
        /// 删除深加工信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var result = await _db.Updateable<ProcessingInfo>()
                .SetColumns(x => new ProcessingInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto)
        {
            var entity = _mapper.Map<EnterpriseInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto)
        {
            var entity = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteEnterpriseAsync(int id)
        {
            var result = await _db.Updateable<EnterpriseInfo>()
                .SetColumns(x => new EnterpriseInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }

        /// <summary>
        /// 创建供应商信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<SupplierResponseDto> CreateSupplierAsync(CreateSupplierDto createDto)
        {
            var entity = _mapper.Map<SupplierInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<SupplierResponseDto>(entity);
        }

        /// <summary>
        /// 更新供应商信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<SupplierResponseDto?> UpdateSupplierAsync(UpdateSupplierDto updateDto)
        {
            var entity = await _db.Queryable<SupplierInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<SupplierResponseDto>(entity);
        }

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteSupplierAsync(int id)
        {
            var result = await _db.Updateable<SupplierInfo>()
                .SetColumns(x => new SupplierInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }
    }

    /// <summary>
    /// 屠宰信息服务接口
    /// </summary>
    public interface ISlaughterService
    {
        /// <summary>
        /// 根据溯源信息ID获取屠宰信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>屠宰信息</returns>
        Task<SlaughterResponseDto?> GetByTraceabilityIdAsync(int traceabilityId);

        /// <summary>
        /// 根据ID获取屠宰信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>屠宰信息</returns>
        Task<SlaughterResponseDto?> GetByIdAsync(int id);

        /// <summary>
        /// 创建屠宰信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<SlaughterResponseDto> CreateAsync(CreateSlaughterDto createDto);

        /// <summary>
        /// 更新屠宰信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<SlaughterResponseDto?> UpdateAsync(UpdateSlaughterDto updateDto);

        /// <summary>
        /// 删除屠宰信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteAsync(int id);

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto);

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto);

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteEnterpriseAsync(int id);
    }

    /// <summary>
    /// 屠宰信息服务实现
    /// </summary>
    public class SlaughterService : ISlaughterService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;

        public SlaughterService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        /// <summary>
        /// 根据溯源信息ID获取屠宰信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>屠宰信息</returns>
        public async Task<SlaughterResponseDto?> GetByTraceabilityIdAsync(int traceabilityId)
        {
            var entity = await _db.Queryable<SlaughterInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<SlaughterResponseDto>(entity);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && x.StageType == "屠宰" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            return response;
        }

        /// <summary>
        /// 根据ID获取屠宰信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>屠宰信息</returns>
        public async Task<SlaughterResponseDto?> GetByIdAsync(int id)
        {
            var entity = await _db.Queryable<SlaughterInfo>()
                .Where(x => x.Id == id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<SlaughterResponseDto>(entity);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == entity.TraceabilityId && x.StageType == "屠宰" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            return response;
        }

        /// <summary>
        /// 创建屠宰信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<SlaughterResponseDto> CreateAsync(CreateSlaughterDto createDto)
        {
            var entity = _mapper.Map<SlaughterInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<SlaughterResponseDto>(entity);
        }

        /// <summary>
        /// 更新屠宰信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<SlaughterResponseDto?> UpdateAsync(UpdateSlaughterDto updateDto)
        {
            var entity = await _db.Queryable<SlaughterInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<SlaughterResponseDto>(entity);
        }

        /// <summary>
        /// 删除屠宰信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var result = await _db.Updateable<SlaughterInfo>()
                .SetColumns(x => new SlaughterInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto)
        {
            var entity = _mapper.Map<EnterpriseInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto)
        {
            var entity = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteEnterpriseAsync(int id)
        {
            var result = await _db.Updateable<EnterpriseInfo>()
                .SetColumns(x => new EnterpriseInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }
    }
}
